using System;
using System.Collections.Generic;
using System.IO;

namespace QuotePoll
{
    /// <summary>
    /// Parses srv file and pulls out address and port
    /// </summary>
    internal class HostAndPort
    {
        public static KeyValuePair<string, int> Parse(string serverFilePath)
        {
            if (!File.Exists(serverFilePath))
            {
                throw new Exception(serverFilePath + " was not found");
            }
            KeyValuePair<string, int> result;
            try
            {
                byte[] array = File.ReadAllBytes(serverFilePath);
                int num = 216;
                string text = "";
                while (array[num] != 0 && array[num] != 58)
                {
                    text += (char) array[num++];
                }
                int value;
                if (array[num] == 0)
                {
                    value = 443;
                }
                else
                {
                    num++;
                    string text2 = "";
                    while (array[num] != 0)
                    {
                        text2 += (char) array[num++];
                    }
                    value = int.Parse(text2);
                }
                result = new KeyValuePair<string, int>(text.Trim(), value);
            }
            catch (Exception)
            {
                throw new Exception("Unable to parse the host and port from SRV");
            }
            return result;
        }
    }
}